package com.huawei.hwid.api.common.apkimpl;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import com.huawei.cloudservice.CloudAccountManager;
import com.huawei.cloudservice.CloudRequestHandler;
import com.huawei.cloudservice.IntentResultHandler;
import com.huawei.cloudservice.LoginHandler;
import com.huawei.hms.api.HuaweiApiAvailability;
import com.huawei.hwid.api.common.h;
import com.huawei.hwid.api.common.k;
import com.huawei.hwid.api.common.l;
import com.huawei.hwid.api.common.o;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.hwid.core.d.b;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.datatype.HwAccount;
import com.huawei.hwid.core.helper.handler.ErrorStatus;
import com.huawei.hwid.d;
import com.huawei.hwid.update.d.j;
import com.huawei.hwid.usecase.GetAccountStatusUseCase;
import com.huawei.hwid.usecase.GetAccountStatusUseCase.RequestValues;

public class a {
    public static void a(Context context, String str, String str2, Bundle bundle, LoginHandler loginHandler) {
        e.b("APKCloudAccountImpl", "getAccountsByType use the apk", true);
        boolean z = bundle.getBoolean(CloudAccountManager.KEY_AIDL);
        boolean c = b.c(context, "com.huawei.hwid.ICloudService");
        e.b("APKCloudAccountImpl", "a_s:" + z + "_" + c, true);
        if (z && c) {
            z = bundle.getBoolean(CloudAccountManager.KEY_CHECK_SIM_STATUS);
            e.b("APKCloudAccountImpl", "css:" + z, true);
            if (z) {
                final Context context2 = context;
                final String str3 = str;
                final String str4 = str2;
                final Bundle bundle2 = bundle;
                final LoginHandler loginHandler2 = loginHandler;
                a(context, new com.huawei.hwid.UseCase.a() {
                    public void a(Bundle bundle) {
                        if ("blocked".equals(bundle.getString("accountstatus"))) {
                            a.b(context2, str3, str4, bundle2);
                        } else {
                            a.b(context2, str3, bundle2, loginHandler2);
                        }
                    }

                    public void b(Bundle bundle) {
                        e.b("APKCloudAccountImpl", "getAccountsByType onError", true);
                    }
                });
                return;
            }
            b(context, str, bundle, loginHandler);
            return;
        }
        b(context, str, str2, bundle);
    }

    private static void b(Context context, String str, Bundle bundle, LoginHandler loginHandler) {
        e.b("APKCloudAccountImpl", "startLoginByAIDL", true);
        com.huawei.hwid.api.common.a a = com.huawei.hwid.api.common.a.a(context);
        if (a == null) {
            e.b("APKCloudAccountImpl", "manager is null", true);
        } else {
            a.a(new l(context, str, bundle, loginHandler));
        }
    }

    public static void a(Context context, Intent intent, int i, CloudRequestHandler cloudRequestHandler) {
        a(context, intent, i, 0, cloudRequestHandler);
    }

    public static void a(Context context, Intent intent, int i, int i2, CloudRequestHandler cloudRequestHandler) {
        if (b.c(context, "com.huawei.hwid.ICloudService")) {
            e.d("APKCloudAccountImpl", "intent" + intent.getAction(), false);
            h hVar = new h(context, intent, i, i2);
            hVar.a(cloudRequestHandler);
            hVar.a();
            return;
        }
        e.d("APKCloudAccountImpl", "no remote interface call service", true);
        cloudRequestHandler.onError(new ErrorStatus(38, "no remote interface call service"));
    }

    private static void b(Context context, String str, String str2, Bundle bundle) {
        e.b("APKCloudAccountImpl", "startAPKByOldWay", true);
        Intent intent = new Intent();
        intent.setClass(context, DummyActivity.class);
        intent.putExtra("requestTokenType", str);
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putString("accountName", str2);
        bundle.putBoolean("isFromApk", true);
        intent.putExtra(HwAccountConstants.EXTRA_BUNDLE, bundle);
        intent.setFlags(1048576);
        b.a(context, intent, 0);
    }

    public static boolean a(Context context) {
        Cursor query;
        boolean z;
        Account[] accountsByType;
        Throwable th;
        boolean z2 = true;
        try {
            query = context.getContentResolver().query(Uri.parse(HwAccountConstants.CONTENT_HASLOGIN_URL), null, null, null, null);
            if (query != null) {
                try {
                    if (query.moveToFirst()) {
                        if (1 == query.getInt(query.getColumnIndex("hasLogin"))) {
                            z = true;
                        } else {
                            z = false;
                        }
                        if (query != null) {
                            query.close();
                        }
                        return z;
                    }
                } catch (RuntimeException e) {
                    try {
                        accountsByType = AccountManager.get(context).getAccountsByType("com.huawei.hwid");
                        if (accountsByType != null || accountsByType.length <= 0) {
                            z = false;
                        } else {
                            z = true;
                        }
                        if (query != null) {
                            query.close();
                        }
                        return z;
                    } catch (Throwable th2) {
                        th = th2;
                        if (query != null) {
                            query.close();
                        }
                        throw th;
                    }
                } catch (Exception e2) {
                    accountsByType = AccountManager.get(context).getAccountsByType("com.huawei.hwid");
                    if (accountsByType != null || accountsByType.length <= 0) {
                        z = false;
                    } else {
                        z = true;
                    }
                    if (query != null) {
                        query.close();
                    }
                    return z;
                }
            }
            accountsByType = AccountManager.get(context).getAccountsByType("com.huawei.hwid");
            if (accountsByType == null || accountsByType.length <= 0) {
                z2 = false;
            }
            z = z2;
            if (query != null) {
                query.close();
            }
        } catch (RuntimeException e3) {
            query = null;
            accountsByType = AccountManager.get(context).getAccountsByType("com.huawei.hwid");
            if (accountsByType != null) {
            }
            z = false;
            if (query != null) {
                query.close();
            }
            return z;
        } catch (Exception e4) {
            query = null;
            accountsByType = AccountManager.get(context).getAccountsByType("com.huawei.hwid");
            if (accountsByType != null) {
            }
            z = false;
            if (query != null) {
                query.close();
            }
            return z;
        } catch (Throwable th3) {
            th = th3;
            query = null;
            if (query != null) {
                query.close();
            }
            throw th;
        }
        return z;
    }

    public static long b(Context context) {
        long j;
        e.b("APKCloudAccountImpl", "getLoginCount", true);
        Cursor query = context.getContentResolver().query(Uri.parse("content://com.huawei.hwid.api.provider/LOGIN_COUNT"), null, null, null, null);
        if (query == null || !query.moveToFirst()) {
            j = -1;
        } else {
            j = query.getLong(query.getColumnIndex("LOGIN_COUNT"));
        }
        if (query != null) {
            query.close();
        }
        return j;
    }

    public static void a(Context context, String str, LoginHandler loginHandler) {
        e.e("APKCloudAccountImpl", "apk---quickLogin()", true);
        Intent intent;
        if (b.b(context, HwAccountConstants.ACTION_QUICK_REGISTER)) {
            com.huawei.hwid.api.common.e.a(context, loginHandler, null);
            intent = new Intent(HwAccountConstants.ACTION_QUICK_REGISTER);
            intent.setPackage("com.huawei.hwid");
            intent.putExtra("requestTokenType", str);
            b.a(context, intent, 0);
        } else if (!b.b(context, "com.huawei.hwid.opensdk.ACTION_QUICKLOGIN_SMS")) {
            e.b("APKCloudAccountImpl", "There is no quick login in APK", true);
            loginHandler.onError(new ErrorStatus(21, "hwid not support quick login"));
        } else if (a(context)) {
            Bundle bundle = new Bundle();
            try {
                bundle.putInt(CloudAccountManager.KEY_LOGIN_CHANNEL, Integer.parseInt(com.huawei.hwid.core.d.a.a(context, str)));
            } catch (Exception e) {
                e.d("APKCloudAccountImpl", "parse AppChannel error", true);
            }
            com.huawei.hwid.api.common.e.a(context, str, bundle, loginHandler);
        } else {
            com.huawei.hwid.api.common.e.a(context, loginHandler, "com.huawei.hwid");
            intent = new Intent("com.huawei.hwid.opensdk.ACTION_QUICKLOGIN_SMS");
            intent.setPackage("com.huawei.hwid");
            intent.putExtra("packname", str);
            b.a(context, intent, 0);
        }
    }

    public static void a(Context context, HwAccount hwAccount) {
        AccountManager.get(context).invalidateAuthToken("com.huawei.hwid", hwAccount.g());
    }

    public static boolean a(Context context, Bundle bundle) {
        boolean z;
        Cursor cursor;
        Throwable th;
        if (context == null) {
            e.b("APKCloudAccountImpl", "context is null", true);
            return false;
        }
        Cursor query;
        try {
            query = context.getContentResolver().query(Uri.parse("content://com.huawei.hwid.api.provider/is_support_fingerprint/1"), null, null, null, null);
            if (query != null) {
                try {
                    if (query.moveToFirst()) {
                        if (1 == query.getInt(query.getColumnIndex("isSupport"))) {
                            z = true;
                        } else {
                            z = false;
                        }
                        if (query != null) {
                            query.close();
                        }
                        return z;
                    }
                } catch (RuntimeException e) {
                    cursor = query;
                    try {
                        e.d("APKCloudAccountImpl", "RuntimeException", true);
                        if (cursor != null) {
                            cursor.close();
                            z = false;
                            return z;
                        }
                        z = false;
                        return z;
                    } catch (Throwable th2) {
                        Throwable th3 = th2;
                        query = cursor;
                        th = th3;
                        if (query != null) {
                            query.close();
                        }
                        throw th;
                    }
                } catch (Exception e2) {
                    try {
                        e.d("APKCloudAccountImpl", "Exception", true);
                        if (query != null) {
                            query.close();
                            z = false;
                            return z;
                        }
                        z = false;
                        return z;
                    } catch (Throwable th4) {
                        th = th4;
                        if (query != null) {
                            query.close();
                        }
                        throw th;
                    }
                }
            }
            z = false;
            if (query != null) {
                query.close();
            }
        } catch (RuntimeException e3) {
            cursor = null;
            e.d("APKCloudAccountImpl", "RuntimeException", true);
            if (cursor != null) {
                cursor.close();
                z = false;
                return z;
            }
            z = false;
            return z;
        } catch (Exception e4) {
            query = null;
            e.d("APKCloudAccountImpl", "Exception", true);
            if (query != null) {
                query.close();
                z = false;
                return z;
            }
            z = false;
            return z;
        } catch (Throwable th5) {
            th = th5;
            query = null;
            if (query != null) {
                query.close();
            }
            throw th;
        }
        return z;
    }

    private static void a(Context context, com.huawei.hwid.UseCase.a aVar) {
        if (context == null) {
            e.b("APKCloudAccountImpl", "context is null", true);
        } else {
            new com.huawei.hwid.b(d.a()).a(new GetAccountStatusUseCase(context), new RequestValues(), aVar);
        }
    }

    public static void b(Context context, Bundle bundle) {
        int i;
        int i2 = 20300000;
        e.b("APKCloudAccountImpl", "downloadHwIDAPK", true);
        if (bundle != null) {
            i = bundle.getInt(CloudAccountManager.KEY_REQUESTCODE);
        } else {
            i = 0;
        }
        int d = o.d(context);
        if (o.b(context) && !o.a(context, 20300000)) {
            i2 = d + 1;
        }
        e.b("APKCloudAccountImpl", "versionCode:" + i2, false);
        j jVar = new j();
        jVar.a("com.huawei.hwid");
        jVar.a(i2);
        jVar.b(HuaweiApiAvailability.APPID_HMS);
        jVar.c(context.getString(com.huawei.hwid.core.d.l.a(context, "cs_update_title")));
        com.huawei.hwid.update.c.a.a(context, i, jVar);
    }

    public static void a(Context context, String str, String str2, Bundle bundle, IntentResultHandler intentResultHandler) {
        com.huawei.hwid.api.common.a a = com.huawei.hwid.api.common.a.a(context);
        if (a == null) {
            e.b("APKCloudAccountImpl", "manager is null", true);
        } else {
            a.a(new com.huawei.hwid.api.common.j(context, str, str2, bundle, intentResultHandler));
        }
    }

    public static void a(Context context, String str, CloudRequestHandler cloudRequestHandler) {
        com.huawei.hwid.api.common.a a = com.huawei.hwid.api.common.a.a(context);
        if (a == null) {
            e.b("APKCloudAccountImpl", "manager is null", true);
        } else {
            a.a(new k(context, str, cloudRequestHandler));
        }
    }
}
